﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.Security;
using br.com.SistemaChamados.Model.Common;
using System.Security.Cryptography;
using System.Text;

namespace br.com.SistemaChamados.Util
{
    public static class SecurityUtils
    {
        public static HttpCookie createATicketValidation(User user)
        {
            FormsAuthentication.Initialize();
            FormsAuthenticationTicket aTicket;
            int versionTicket = generateRandonNumber();
            string loginUser = user.EMail;
            DateTime ticketEmission = DateTime.Now;
            DateTime ticketExpiration = DateTime.Now.AddMinutes(10);
            bool persistenceTicketContextInSession = false;
            string profileContext = user.Profile;
            string ticketPath = FormsAuthentication.FormsCookiePath;
            aTicket = new FormsAuthenticationTicket(versionTicket,
                                                    loginUser,
                                                    ticketEmission,
                                                    ticketExpiration,
                                                    persistenceTicketContextInSession,
                                                    profileContext,
                                                    ticketPath);
            string hashTicket = generateHashFromTicket(aTicket);
            HttpCookie authenticationCookie = createAAuthenticationCookie(aTicket, hashTicket);
            return authenticationCookie;

        }

        public static int generateRandonNumber()
        {
            return (new Random()).Next(0, 1000);
        }

        public static string createASHA1Hash(string inputString)
        {
            string encryptContent = "";
            SHA1 sha1Alg = new SHA1CryptoServiceProvider();
            byte[] dataToCrypt = System.Text.Encoding.ASCII.GetBytes(inputString);
            byte[] generatedHash = sha1Alg.ComputeHash(dataToCrypt);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < generatedHash.Length; i++)
            {
                stringBuilder.Append(generatedHash[i].ToString("X2"));
            }
            encryptContent = stringBuilder.ToString();
            return encryptContent;
        }

        private static string generateHashFromTicket(FormsAuthenticationTicket aTicket)
        {
            return FormsAuthentication.Encrypt(aTicket);
        }

        private static HttpCookie createAAuthenticationCookie(FormsAuthenticationTicket aTicket, string hashTicket)
        {
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
            if (aTicket.IsPersistent) cookie.Expires = aTicket.Expiration;
            return cookie;
        }

        private static int randomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private static string randomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        public static string createRandomicPassword()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(randomString(4, true));
            sb.Append(randomNumber(1000, 9999));
            sb.Append(randomString(2, false));
            return sb.ToString();
        }

    }
}